home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / filedll.com / FDLGDLL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-04-23  |  5.5 KB  |  204 lines

  1. library Fdlgdll;
  2.  
  3. {$R FILEDLGS}
  4.  
  5.  
  6.  
  7. uses WinTypes, WinProcs, WinDos, Strings;
  8.  
  9.  
  10. const
  11.   id_FName = 100;
  12.   id_FPath = 101;
  13.   id_FList = 102;
  14.   id_DList = 103;
  15.  
  16. const
  17.   fsFileSpec = fsFileName + fsExtension;
  18.  
  19. type
  20.   TDWord = record
  21.     Lo, Hi: Word;
  22.   end;
  23.  
  24. var
  25.   GCaption: PChar;
  26.   GFilePath: PChar;
  27.   GPathName: array[0..fsPathName] of Char;
  28.   GExtension: array[0..fsExtension] of Char;
  29.   GFileSpec: array[0..fsFileSpec] of Char;
  30.  
  31. function GetFileName(FilePath: PChar): PChar;
  32. var
  33.   P: PChar;
  34. begin
  35.   P := StrRScan(FilePath, '\');
  36.   if P = nil then P := StrRScan(FilePath, ':');
  37.   if P = nil then GetFileName := FilePath else GetFileName := P + 1;
  38. end;
  39.  
  40. function GetExtension(FilePath: PChar): PChar;
  41. var
  42.   P: PChar;
  43. begin
  44.   P := StrScan(GetFileName(FilePath), '.');
  45.   if P = nil then GetExtension := StrEnd(FilePath) else GetExtension := P;
  46. end;
  47.  
  48. function FileDialog(Dialog: HWnd; Message, WParam: Word;
  49.   LParam: TDWord): Bool; export;
  50. var
  51.   PathLen: Word;
  52.   P: PChar;
  53.  
  54. procedure UpdateFileName;
  55. begin
  56.   SetDlgItemText(Dialog, id_FName, StrLower(GPathName));
  57.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  58. end;
  59.  
  60. procedure SelectFileName;
  61. begin
  62.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  63.   SetFocus(GetDlgItem(Dialog, id_FName));
  64. end;
  65.  
  66. function UpdateListBoxes: Boolean;
  67. var
  68.   Result: Integer;
  69.   Path: array[0..fsPathName] of Char;
  70. begin
  71.   UpdateListBoxes := False;
  72.   if GetDlgItem(Dialog, id_FList) <> 0 then
  73.   begin
  74.     StrCopy(Path, GPathName);
  75.     Result := DlgDirList(Dialog, Path, id_FList, id_FPath, 0);
  76.     if Result <> 0 then DlgDirList(Dialog, '*.*', id_DList, 0, $C010);
  77.   end else
  78.   begin
  79.     StrLCopy(Path, GPathName, GetFileName(GPathName) - GPathName);
  80.     StrLCat(Path, '*.*', fsPathName);
  81.     Result := DlgDirList(Dialog, Path, id_DList, id_FPath, $C010);
  82.   end;
  83.   if Result <> 0 then
  84.   begin
  85.     StrLCopy(GFileSpec, GetFileName(GPathName), fsFileSpec);
  86.     StrCopy(GPathName, GFileSpec);
  87.     UpdateFileName;
  88.     UpdateListBoxes := True;
  89.   end;
  90. end;
  91.  
  92. begin
  93.   FileDialog := True;
  94.   case Message of
  95.     wm_InitDialog:
  96.       begin
  97.         SendDlgItemMessage(Dialog, id_FName, em_LimitText, fsPathName, 0);
  98.         if GCaption <> nil then SetWindowText(Dialog, GCaption);
  99.         StrLCopy(GPathName, GFilePath, fsPathName);
  100.         StrLCopy(GExtension, GetExtension(GPathName), fsExtension);
  101.         if not UpdateListBoxes then
  102.         begin
  103.           StrCopy(GPathName, '*.*');
  104.           UpdateListBoxes;
  105.         end;
  106.         SelectFileName;
  107.         Exit;
  108.       end;
  109.     wm_Command:
  110.       case WParam of
  111.         id_FName:
  112.           begin
  113.             if LParam.Hi = en_Change then
  114.               EnableWindow(GetDlgItem(Dialog, id_Ok),
  115.                 SendMessage(LParam.lo, wm_GetTextLength, 0, 0) <> 0);
  116.             Exit;
  117.           end;
  118.         id_FList:
  119.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  120.           begin
  121.             DlgDirSelect(Dialog, GPathName, id_FList);
  122.             UpdateFileName;
  123.             if LParam.Hi = lbn_DblClk then
  124.               SendMessage(Dialog, wm_Command, id_Ok, 0);
  125.             Exit;
  126.           end;
  127.         id_DList:
  128.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  129.           begin
  130.             DlgDirSelect(Dialog, GPathName, id_DList);
  131.             StrCat(GPathName, GFileSpec);
  132.             if LParam.Hi = lbn_DblClk then
  133.               UpdateListBoxes else
  134.               UpdateFileName;
  135.             Exit;
  136.           end;
  137.         id_Ok:
  138.           begin
  139.             GetDlgItemText(Dialog, id_FName, GPathName, fsPathName + 1);
  140.             FileExpand(GPathName, GPathName);
  141.             PathLen := StrLen(GPathName);
  142.             if (GPathName[PathLen - 1] = '\') or
  143.               (StrScan(GPathName, '*') <> nil) or
  144.               (StrScan(GPathName, '?') <> nil) or
  145.               (GetFocus = GetDlgItem(Dialog, id_DList)) then
  146.             begin
  147.               if GPathName[PathLen - 1] = '\' then
  148.                 StrLCat(GPathName, GFileSpec, fsPathName);
  149.               if not UpdateListBoxes then
  150.               begin
  151.                 MessageBeep(0);
  152.                 SelectFileName;
  153.               end;
  154.               Exit;
  155.             end;
  156.             StrLCat(StrLCat(GPathName, '\', fsPathName),
  157.               GFileSpec, fsPathName);
  158.             if UpdateListBoxes then Exit;
  159.             GPathName[PathLen] := #0;
  160.             if GetExtension(GPathName)[0] = #0 then
  161.               StrLCat(GPathName, GExtension, fsPathName);
  162.             StrLower(StrCopy(GFilePath, GPathName));
  163.             EndDialog(Dialog, 1);
  164.             Exit;
  165.           end;
  166.         id_Cancel:
  167.           begin
  168.             EndDialog(Dialog, 0);
  169.             Exit;
  170.           end;
  171.       end;
  172.   end;
  173.   FileDialog := False;
  174. end;
  175.  
  176. function DoFileDialog(Window: HWnd;
  177.   FilePath, DialogName, Caption: PChar): Boolean; export;
  178. var
  179.   DialogProc: TFarProc;
  180. begin
  181.   GFilePath := FilePath;
  182.   GCaption := Caption;
  183.   DialogProc := MakeProcInstance(@FileDialog, HInstance);
  184.   DoFileDialog := DialogBox(HInstance, DialogName, Window, DialogProc) = 1;
  185.   FreeProcInstance(DialogProc);
  186. end;
  187.  
  188. function DoFileOpen(Window: HWnd; FilePath: PChar): Boolean; export;
  189. begin
  190.   DoFileOpen := DoFileDialog(Window, FilePath, 'FileOpen', nil);
  191. end;
  192.  
  193. function DoFileSave(Window: HWnd; FilePath: PChar): Boolean; export;
  194. begin
  195.   DoFileSave := DoFileDialog(Window, FilePath, 'FileSave', nil);
  196. end;
  197.  
  198. exports DoFileDialog index 1;
  199. exports DoFileOpen   index 2;
  200. exports DoFileSave   index 3;
  201.  
  202. begin
  203. end.
  204.